package com.gudaomai.ekproject.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.gudaomai.ekproject.common.CommonConstant;
import com.gudaomai.ekproject.common.CommonException;
import com.gudaomai.ekproject.common.MsgConstant;
import com.gudaomai.ekproject.entity.Role;
import com.gudaomai.ekproject.entity.Sysuser;
import com.gudaomai.ekproject.entity.UserRole;
import com.gudaomai.ekproject.mapper.UserRoleMapper;
import com.gudaomai.ekproject.service.IRoleService;
import com.gudaomai.ekproject.service.ISysuserService;
import com.gudaomai.ekproject.service.IUserRoleService;
import com.gudaomai.ekproject.service.impl.BaseServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * user_role 服务实现类
 * </p>
 *
 * @author zenghualu
 * @since 2017-06-26
 */
@Service
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

	@Autowired
	protected MessageSource messageSource;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	ISysuserService sysuserServ;
	@Autowired
	IRoleService roleServ;

	/**
	 * 添加用户角色关系
	 * 
	 * 0.1 判断传入的用户是否存在 //TODO-complete:增加逻辑 0.2判断传入的角色是否存在 1.check是否用户角色关系已经存在
	 * 如果存在抛异常 2.添加到用户角色表
	 * 
	 */
	public void add(Sysuser user, Role role) throws CommonException {

		// 检查数据库是否存在该条记录
		Sysuser sysuserTemp = sysuserServ.selectById(user);
		Role roleTemp = roleServ.selectById(role);

		// 如果查询到数据，则中断插入操作、抛出异常
		if (null == sysuserTemp || null == roleTemp || isExist(user.getId(), role.getId())) {
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED,
					getMessage(MsgConstant.RET_USER_ROLR_ADD_ALREADY_EXIST));
		}

		// 若存在、执行插入操作
		// link to see: RolesPermissionsServicesImpl add()
		UserRole userRole = new UserRole();
		userRole.setId(IdWorker.get32UUID());
		userRole.setUid(user.getId());
		userRole.setRoleId(role.getId());

		// 插入该记录到数据库
		this.insert(userRole);

	}

	//通过uid获取用户角色
	@Override
	public List<UserRole> getRolesByUserId(String uid) throws CommonException {

//		List<UserRole> userRoles = userRoleMapper.getUserRoleByUid(uid);

		EntityWrapper<UserRole> ew=new EntityWrapper<>();
		ew.setEntity(new UserRole());
		ew.eq("uid", uid);
		
		List<UserRole> userRoles=this.selectList(ew);
		
		if (userRoles.size() > 0) {
			return userRoles;
		} else {
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_FOUND,
					getMessage(MsgConstant.RET_USER_ROLE_SELECT_NOTEXIST));
		}

	}

	//通过角色id,用户id,判断用户角色是否存在
	@Override
	public boolean isExist(String uid, String rid) throws CommonException {
		
//		UserRole userRole=this.userRoleMapper.getUserRoleByUidRid(uid, rid);
		
		EntityWrapper<UserRole> ew=new EntityWrapper<>();
		ew.setEntity(new UserRole());
		ew.eq("uid", uid).eq("role_id", rid);
		
		UserRole userRole=this.selectOne(ew);
		
		if (null==userRole) {
			return false;
		}
		return true;
	}

}
